package models

import (
	"gorm.io/gorm"
	"time"
	models2 "tuowei.com/app/api/models"
	"tuowei.com/common/models"
)

type FollowDirection int

const (
	LossFollowDirection    = 1 //流失
	AbandonFollowDirection = 2 // 放弃
	VisitorFollowDirection = 3 // 游客
)

//var FollowDirectionMap = map[FollowDirection]string{
//	LossFollowDirection:    "流失不再为其提供服务",
//	AbandonFollowDirection: "放弃",
//	VisitorFollowDirection: "成为游客",
//}
var FollowDirectionMap = map[string]FollowDirection{
	"流失不再为其提供服务": LossFollowDirection,
	"放弃":         AbandonFollowDirection,
	"成为游客":       VisitorFollowDirection,
}

type SysStudent struct {
	models.Model
	SerialNum           string        `json:"serial_num" gorm:"varchar(20);comment:学员编号"`
	Name                string        `json:"name" gorm:"type:varchar(50);not null;comment:学员名称"`
	CollegeId           int           `json:"college_id" gorm:"type:int(5);not null;default:0;comment:学员院校ID"`
	NickName            string        `json:"nick_name" gorm:"type:varchar(100);comment:昵称"`
	Phone               string        `json:"phone" gorm:"type:varchar(20);not null;comment:手机号码"`
	TotalPay            float32       `json:"total_pay" gorm:"type:decimal(10,2);comment:付费总额"`
	Balance             float32       `json:"balance" gorm:"type:decimal(10,2);comment:学员余额"`
	UserLabel           string        `json:"user_label" gorm:"type:varchar(50);comment:学员标识 线上/线下"`
	OrderCount          int           `json:"order_count" gorm:"type:int(10);comment:订单总数"`
	UserType            string        `json:"user_type" gorm:"varchar(10);comment:用户类型 普通/VIP/SVIP"`
	Province            int           `json:"province" gorm:"type:int(10);comment:所属大区"`
	City                int           `json:"city" gorm:"type:int(10);comment:所属区域"`
	District            int           `json:"district" gorm:"type:int(10);comment:所属区县"`
	Area                int           `json:"area" gorm:"type:int(10);comment:所属教学点"`
	ShenBenTime         time.Time     `json:"shen_ben_time"`
	ShenBenYear         string        `json:"shen_ben_year" gorm:"-"`
	LastPayTime         time.Time     `json:"last_pay_time"`
	TeachRecommender    int           `json:"teach_recommender" gorm:"type:int(10);comment:老师推荐人ID"`
	Recommender         int           `json:"recommender" gorm:"type:int(10);comment:推荐人"`
	RmId                int           `json:"rm_id" gorm:"type:smallint(2);comment:大区经理ID"`
	RegionalMId         int           `json:"regional_m_id" gorm:"type:smallint(2);comment:区域经理ID"`
	EnrollmentTeacherId int           `json:"enrollment_teacher_id" gorm:"type:smallint(2);comment:招生老师ID"`
	ClassId             int           `json:"class_id" gorm:"type:smallint(2);comment:班级ID"`
	Source              string        `json:"source" gorm:"type:varchar(20);comment:学员来源"`
	Status              int           `json:"status" gorm:"type:tinyint(1);comment:学员状态 1为启用状态 0为禁用状态"`
	BuyStatus           int           `json:"buy_status" gorm:"type:tinyint(1);default 1;comment:学生购买状态，1-游客，2-正式，3-结业，4-退学"`
	Divided             int           `json:"divided" gorm:"type:tinyint(1);not null;default:0;comment:学员分班状态，默认0未分班，1-已分班"`
	ProvinceRegion      SysRegion     `json:"province_region" gorm:"foreignKey:province;references:id"`
	CityDept            *CityDept     `json:"city_dept" gorm:"foreignKey:city;references:dept_id"`
	DistrictDept        *DistrictDept `json:"district_dept"  gorm:"foreignKey:district;references:dept_id"`
	//AreaDept            *AreaDept             `json:"area_dept" gorm:"foreignKey:area;references:dept_id"`
	AreaPoint         TeachingPoint         `json:"area_point" gorm:"foreignKey:area;references:id"`
	Recommenders      *StudentBinding       `json:"recommenders" gorm:"foreignKey:recommender;references:id"`
	TeachRecommenders *Binding              `json:"teach_recommenders" gorm:"foreignKey:teach_recommender;references:user_id"`
	RegionalManagers  *Binding              `json:"regional_managers" gorm:"foreignKey:rm_id;references:user_id"`
	RegionalManager   *Binding              `json:"regional_manager" gorm:"foreignKey:regional_m_id;references:user_id"`
	AdmissionsOfficer *Binding              `json:"admissions_officer" gorm:"foreignKey:enrollment_teacher_id;references:user_id"`
	Faculty           string                `json:"faculty" gorm:"type:varchar(20);comment:院系"`
	Major             string                `json:"major" gorm:"type:varchar(50);comment:专业"`
	College           SysColleges           `json:"college" gorm:"foreignKey:college_id;references:id"`
	IsRegistered      int                   `json:"is_registered" gorm:"type:tinyint(2);comment:是否完成注册信息填写 -1-没有完成注册信息，1-完成注册信息"`
	State             int                   `json:"state" gorm:"type:tinyint(2);default:0;comment:学习状态"`
	StateInfo         *SysStudentState      `json:"state_info" gorm:"foreignKey:state;references:id"`
	IdCard            string                `json:"id_card" gorm:"type:varchar(50);comment:身份证ID"`
	WxStudents        *models2.SysWxStudent `json:"-" gorm:"foreignKey:id;references:student_id"` // 关联微信信息
	QqStudents        *models2.SysQqStudent `json:"-" gorm:"foreignKey:id;references:student_id"` // 关联qq信息
	CollegeClass      string                `json:"college_class" gorm:"type:varchar(20);comment:大学所在班级"`
	QQ                string                `json:"qq" gorm:"type:varchar(20);comment:QQ号"`
	Grade             string                `gorm:"type:varchar(200);default:'';comment:年级" json:"grade"`
	KeBie             string                `gorm:"type:varchar(200);default:'';comment:科别" json:"ke_bie"`
	FollowDirection   int                   `gorm:"type:tinyint(1);default:0;comment:学员去向,1-流失，2-放弃，3-成为游客" json:"follow_direction"`
	ProgressStatus    int                   `json:"progress_status" gorm:"type:tinyint(1);not null;default:1;comment:学员跟进状态，1-刚添加的学生，2-待联系，3-待分班，4-完成"`
	Explain           string                `json:"explain" gorm:"type:varchar(50);comment:食宿说明"`
	TrainingDays      int                   `json:"training_days" gorm:"smallint(5);comment:集训天数"`
	Cover             string                `json:"cover" gorm:"varchar(255);comment:头像"`
	Scores            []models2.SysScore    `json:"scores" gorm:"foreignKey:buyer_id;references:id"`
	OfflineOrders     []OfflineOrder        `json:"offline_orders" gorm:"foreignKey:buyer_id;references:id"`  // 关联的线下订单
	OnlineOrders      []OnlineOrder         `json:"online_orders" gorm:"foreignKey:buyer_id;references:id"`   // 关联的线下订单
	MaterialOrders    []MaterialOrder       `json:"material_orders" gorm:"foreignKey:buyer_id;references:id"` // 关联的线下订单
	models.ModelTime
}

func (SysStudent) TableName() string {
	return "sys_student"
}

func (s SysStudent) Able() func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Where("sys_student.status = ?", 0)
	}
}

type StudentBinding struct {
	Id    int    `json:"id"`
	Name  string `json:"name"`
	Phone string `json:"phone"`
}

func (StudentBinding) TableName() string {
	return "sys_student"
}

type SysStudentList struct {
	Id          int          `json:"id"`
	SerialNum   string       `json:"serial_num"`
	Name        string       `json:"name" gorm:"type:varchar(50);not null;comment:学员名称"`
	NickName    string       `json:"nick_name" gorm:"type:varchar(100);comment:昵称"`
	Phone       string       `json:"phone" gorm:"type:varchar(20);not null;comment:手机号码"`
	TotalPay    float32      `json:"total_pay" gorm:"type:decimal(10,2);comment:付费总额"`
	UserLabel   string       `json:"user_label" gorm:"type:varchar(50);comment:学员标识 线上/线下"`
	OrderCount  int          `json:"order_count" gorm:"type:int(10);comment:订单总数"`
	UserType    string       `json:"user_type" gorm:"tinyint(1);comment:用户类型 普通/VIP/SVIP"`
	Province    int          `json:"province" gorm:"type:int(10);comment:所属大区"`
	LastPayTime time.Time    `json:"last_pay_time"`
	Dept        *StudentDept `json:"dept" gorm:"foreignKey:province;references:dept_id"`
	CollegeId   int          `json:"college_id" gorm:"type:int(5);not null;default:0;comment:学员院校ID"`
	College     SysColleges  `json:"college" gorm:"foreignKey:college_id;references:id"`
	Status      int          `json:"status" gorm:"type:tinyint(1);comment:学员状态"`
	CreatedAt   time.Time    `json:"createdAt" gorm:"comment:创建时间"`
}

func (SysStudentList) TableName() string {
	return "sys_student"
}

type TransferStudent struct {
	Name    string  `json:"name"`
	IdCard  string  `json:"id_card" gorm:"type:varchar(50);comment:身份证ID"`
	Balance float64 `json:"balance"`
	Openid  string  `json:"openid"`
}

func (TransferStudent) TableName() string {
	return "sys_student"
}
